package com.studentstreet.adapter;

import java.util.Vector;

import android.content.Context;
import android.content.res.Resources;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.studentstreet.R;
import com.studentstreet.entities.CommentEntity;

public class CommentAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private Vector<CommentEntity> mModel = new Vector<CommentEntity>();
	private Context context;

	public CommentAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public void addComment(String userName, String time, String content,
			int rank) {
		CommentEntity comment = new CommentEntity();
		comment.setContent(content);
		comment.setRank(rank);
		comment.setUser_name(userName);
		comment.setTime(time);
		mModel.add(comment);
	}

	@Override
	public int getCount() {
		return mModel.size();
	}

	@Override
	public Object getItem(int position) {
		if (position >= getCount()) {
			return null;
		}
		return mModel.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.comment_item, null);
		}
		CommentEntity comment = mModel.get(position);
		convertView.setTag(position);
		Resources res = convertView.getResources();

		TextView header = (TextView) convertView
				.findViewById(R.id.comment_header);
		header.setText(getHeader(convertView, comment.getUser_name(),
				comment.getTime()));

		TextView content = (TextView) convertView
				.findViewById(R.id.comment_content);
		content.setText("  " + comment.getContent());

		RatingBar ratingBar = (RatingBar) convertView
				.findViewById(R.id.comment_rank_RatingBar);
		ratingBar.setRating(comment.getRank());

		return convertView;
	}

	private Spanned getHeader(View view, String user, String time) {
		user = "<b><font color='red'>" + user + "</font></b>";
		time = "<font color='#00bbaa'>" + time + "</font>";
		String html = view.getResources().getString(R.string.comment_header,
				user, time);
		return Html.fromHtml(html);
	}
}
